/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ Franz Wilhelmstötter
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 * 	 Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 * 	 
 */
package edu.scut.ga.general;

import static java.lang.String.format;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.jenetics.GeneticAlgorithm;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version $Id: GAUtils.java 705 2010-12-13 21:36:33Z fwilhelm $
 */
public final class GAUtils {

	private GAUtils() {
	}
	
	public static void execute(
		final GeneticAlgorithm<?, ?> ga, 
		final int generations, 
		final int printEveryNthElement
	) {
		Collection<String> gaResult = new ArrayList<String>();
		ga.setup();	
		System.out.println(ga);
		for (int i = 1; i < generations; ++i) {
			ga.evolve();
			if (i%printEveryNthElement == (printEveryNthElement - 1)) {
				gaResult.add(ga.toString());
				System.out.println(ga);
				System.out.flush();	
			}
		}
		
		System.out.println(ga.getTimeStatistics());
		System.out.println(ga.getBestStatistics());
		gaResult.add(ga.getTimeStatistics().toString());
		gaResult.add(ga.getBestStatistics().toString());
		System.out.flush();
		createResultFile("GA_Result.txt", gaResult);
	}

	public static void printConfig(
		final String name, 
		final GeneticAlgorithm<?, ?> ga,
		final int generations,
		final Object... alterers
	) {
		final StringBuilder out = new StringBuilder();
		final String spattern = "| %18s: %-36s|\n";
		final String sspattern = "| %12s %-43s|\n";
		final String ipattern = "| %18s: %-36d|\n";
		
		out.append("+---------------------------------------------------------+\n");
		out.append(format(spattern, "GA", name));
		out.append("+---------------------------------------------------------+\n");
		out.append(format(spattern, "FitnessFunction", ga.getFitnessFunction()));
		out.append(format(ipattern, "Population", ga.getPopulationSize()));
		out.append(format(ipattern, "Generations", generations));
		out.append(format(spattern, "Alterers", ""));
		for (Object alterer : alterers) {
			out.append(format(sspattern, "*", alterer));
		}
		out.append("+---------------------------------------------------------+\n");
		
		
		System.out.print(out);
	}
	
	// 生成结果文件
	public static void createResultFile(String resultFileName, Collection<String> resultNetworkData) {
		
		FileWriter fwTemp = null;
		BufferedWriter bwTemp = null;
		try {
			try {
				fwTemp = new FileWriter(resultFileName);
			} catch (IOException e) {
				e.printStackTrace();
			}
			bwTemp = new BufferedWriter(fwTemp);
			for (String thisData : resultNetworkData) {
				bwTemp.write(thisData);
				bwTemp.newLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				if (bwTemp != null)
					bwTemp.close();
				if (fwTemp != null)
					fwTemp.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		

	}
	
}
